package com.example.charge

import android.app.DatePickerDialog
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.charge.databinding.ActivityChargeAddBinding
import com.example.charge.entity.ChargeEntity
import com.example.common.view.view.BaseMvvmActivity
import java.util.Calendar

@Route(path = "/charge/ChargeAddActivity")
class ChargeAddActivity : BaseMvvmActivity<ActivityChargeAddBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_charge_add
    private var selectedStartDate: String = ""
    private var selectedEndDate: String = ""
    var chargeEntity : ChargeEntity? = null
    override fun initView() {
        super.initView()
        // 收费项目数据
        val chargeTypes = arrayOf("请选择", "物业费", "水电费", "停车费", "维修费", "其他费用")
        val projectAdapter =
            object : ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, chargeTypes) {
                override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                    val view = super.getView(position, convertView, parent)
                    (view as TextView).setTextSize(12f) // 设置字体大小
                    return view
                }

                override fun getDropDownView(
                    position: Int,
                    convertView: View?,
                    parent: ViewGroup
                ): View {
                    val view = super.getDropDownView(position, convertView, parent)
                    (view as TextView).setTextSize(12f)  // 设置下拉列表字体大小
                    return view
                }
            }
        projectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.type.adapter = projectAdapter

        // 初始化时间选择器
        initDatePicker()
        chargeEntity = intent.getParcelableExtra("chargeEntity")


        mDataBinding.btn.setOnClickListener {
            // 获取Spinner选中的收费项目
            val selectedChargeType = chargeTypes[mDataBinding.type.selectedItemPosition]

            val chargeAddEntity = ChargeEntity(
            actualAmount=  mDataBinding.total.text.toString().toInt(),
            balanceDeduction=  0,
            chargeAmount=  mDataBinding.total.text.toString().toInt(),
            chargeType=  mDataBinding.type.selectedItem.toString(),
            contactNumber=  "13000001111",
            discountAmount=  0,
            effectiveDate= selectedStartDate,
            expirationDate= selectedEndDate,
            id=  0,
            operator=  "张三",
            payerId=  1,
            payerName= "李四",
            paymentMethod= "微信",
            paymentStatus= "未缴费",
            paymentTime=  selectedStartDate,
            penalty=  0,
            receivableAmount= mDataBinding.total.text.toString().toInt()
            )
            ARouter.getInstance().build("/charge/ChargeDetailActivity")
                .withParcelable("chargeEntity",chargeEntity)
                .withParcelable("chargeEntityAdd",chargeAddEntity)
                .navigation()
        }
    }

    private fun initDatePicker() {
        // 设置开始日期点击事件
        mDataBinding.root.findViewById<TextView>(R.id.time_on)?.setOnClickListener {
            showDatePickerDialog(true)
        }

        // 设置结束日期点击事件
        mDataBinding.root.findViewById<TextView>(R.id.time_off)?.setOnClickListener {
            showDatePickerDialog(false)
        }
    }

    private fun showDatePickerDialog(isStartDate: Boolean) {
        val calendar = Calendar.getInstance()
        val datePickerDialog = DatePickerDialog(
            this,
            { _, year, month, dayOfMonth ->
                val selectedDate = "${year}-${String.format("%02d", month + 1)}-${String.format("%02d", dayOfMonth)}"

                // 更新对应TextView的文本
                if (isStartDate) {
                    mDataBinding.root.findViewById<TextView>(R.id.time_on)?.text = selectedDate
                    selectedStartDate = selectedDate
                } else {
                    mDataBinding.root.findViewById<TextView>(R.id.time_off)?.text = selectedDate
                    selectedEndDate = selectedDate
                }
            },
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
        )
        datePickerDialog.show()
    }

}